package asionreachel.cn.smarthomedemo.widget.filter;

import android.content.Context;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.ImageView;

/**
 * Created by Simon(lyd) on 2016/9/27:06.
 * 描述：实现香薰灰选功能,只有设置滤镜一个功能
 */

public class FilterImageView3 extends ImageView{

    public FilterImageView3(Context context) {
        this(context,null);
    }

    public FilterImageView3(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public FilterImageView3(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        setFilter1();
        setFilter2();
    }



    /**
     * 设置滤镜
     */
    private void setFilter1() {
        //先获取设置的src图片
        Drawable drawable = getDrawable();
        if (drawable != null) {
            //设置滤镜
            drawable.setColorFilter(Color.GRAY, PorterDuff.Mode.MULTIPLY);
        }
    }

    /**
     * 设置滤镜
     */
    private void setFilter2() {
        //先获取设置的src图片
        Drawable drawable  = getBackground();
        if (drawable != null) {
            //设置滤镜
            drawable.setColorFilter(Color.GRAY, PorterDuff.Mode.MULTIPLY);
        }
    }

    /**
     * 清除滤镜
     */
    private void removeFilter() {
        //先获取设置的src图片
        Drawable drawable = getDrawable();
        //当src图片为Null，获取背景图片
        if (drawable == null) {
            drawable = getBackground();
        }
        if (drawable != null) {
            //清除滤镜
            drawable.clearColorFilter();
        }
    }


}
